use "$dhs2013\phkr61dt\PHKR61FL", clear

	do "edit_dhsvars"
	
	
	rename v006 surveymonth
	rename v007 surveyyear 
	rename v016 surveyday
	rename b1 month
	rename b2 year
	
	rename m15 delivery
	g delivhome=delivery==11 if delivery<99
	g delivout=inlist(delivery,21,22,26,31,36,96) if delivery<99
	g delivpub=inlist(delivery,21,22,26) if delivery<99
	g delivpvt=inlist(delivery,31,36) if delivery<99
	g delivhosp=inlist(delivery,21,31) if delivery < 99
	g delivother=inlist(delivery,12,22,26,36,96) if delivery <99
	
	
	g anyprenat=m2n==0 if m2n<9 /*if you answered "no" to no one*/
	g prenatdoct=m2a if m2a<9
	g prenatother=1 if m2b==1
		replace prenatother=1 if m2c==1
		replace prenatother=1 if m2h==1
		replace prenatother=1 if m2i==1
		replace prenatother=1 if m2k==1
		egen _other=rowtotal(m2a m2b m2c m2h m2i m2k), miss
		replace prenatother=0 if prenatother==. &  _other<.
	g prenatskill=prenatdoct
		replace prenatskill=1 if m2b==1
		replace prenatskill=1 if m2c==1
		
	g nante=m14 if m14<98
	g firstante=m13 if m13 <98
		recode m57* (8 9 =.)
		egen _ante=rowtotal(m57*), miss 
	g antehosp=1 if m57e==1
		replace antehosp=1 if m57m==1
		replace antehosp=0 if antehosp==. & _ante<.


	g anyante=nante>0 if nante<.
	

	**Vaccinations 
	g vaccbcg=s509b if s509b<8
	g vacchepb=s509i if s509i<8
	g vaccdpt=s509l if s509l<8
	g vaccfirstpol=(s509n<=2) if s509n<8
	g vacclastpol=s509p if s509p<8
	g vaccmeas=s509u if s509u<8


	g datebcg=mdy(h2m,1,h2y)
		replace datebcg=mdy(1,1,2015) if h2==0
	g datehepb=mdy(s506hb3m, 1, s506hb3y)
		replace datehepb=mdy(1,1,2015) if s506hb3==0
	g datedpt=mdy(h7m,1,h7y)
		replace datedpt=mdy(1,1,2015) if h7==0
	g datefirstpol=mdy(h4m, 1, h4y)
		replace datefirstpol=mdy(1,1,2015) if h4==0
	g datelastpol=mdy(h8m, 1, h8y)
		replace datelastpol=mdy(1,1,2015) if h8==0
	g datemeas=mdy(h9m, 1, h9y)
		replace datemeas=mdy(1,1,2015) if h9==0
	
	format date* %d
	
	***directly from vaccination booklet
	foreach disease in bcg hepb dpt firstpol lastpol meas {
		replace vacc`disease'=0 if date`disease'-(mdy(month+1,1,year)-1) > 365  & date`disease'<./*last day of birth month*/
		replace vacc`disease'=1 if date`disease'-(mdy(month+1,1,year)-1) <= 365 & date`disease'<.
		replace vacc`disease'=. if childage==0
		g vacchas`disease'=1-missing(vacc`disease') if year>=2008 & childage>0
	} 
	
	egen vaccfull=rowmean(vaccbcg vacchepb vaccdpt  vacclastpol vaccmeas)
	recode vaccfull (0/.9999999=0)
	replace vaccfull=. if vaccbcg+vacchepb+vaccdpt + vacclastpol+vaccmeas==.
	
	egen vaccany=rowmean(vaccbcg vacchepb vaccdpt  vacclastpol vaccmeas)
	recode vaccany (.00000001/1=1)
		
	keep cluster $keyvars month year surveyyear surveymonth   deliv* *ante* *pren*   vacc*  
		
	g provid=string(provcode,"%02.0f")
	g munid=string(provcode,"%02.0f") +string(municode, "%02.0f")
	g baranid=string(provcode,"%02.0f") +string(municode, "%02.0f") +string(barancode, "%03.0f")
		
	merge m:1 year month munid using "merged_ph_ncdc_dhs_raintemp", gen(_rain) keepusing(*temp* *prec*)
		drop if _rain==2
		/*only armm provinces match up*/
		
	merge m:1 provid year month using "bcms_byprov_allph", gen(_prov)
		drop if _prov==2
		
	merge m:1 munid year month using "bcms_bymun_allph", gen(_mun)			
		drop if _mun==2
	
	
	g day=15 /*actual day not given*/
	
	merge m:1 provid year month day using "bcms_byprov_allph_date", gen(_provd) keepusing(provid year month day *270 *a365)
		drop if _provd==2
	
	merge m:1 munid year month day using "bcms_bymun_allph_date", gen(_mund) keepusing(munid year month day *270 *a365)	
		drop if _mund==2
		

		
	destring provid munid baranid, replace
	recode sex (2=0), gen(male)
	

	save "birthhist", replace


	
